package com.dell.chen.servlet;

import com.dell.chen.pojo.Flowers;
import com.dell.chen.pojo.Page;
import com.dell.chen.pojo.Users;
import com.dell.chen.service.FlowerService;
import com.dell.chen.service.Impl.FlowerServiceImpl;
import com.dell.chen.utils.WebUtils;
import com.google.gson.Gson;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author ：ChenwithIU
 * @date ：Created in 2020/11/30 14:51
 */
public class FlowerServlet extends BaseServlet{

    private FlowerService flowerService = new FlowerServiceImpl();

//    /**
//     * 处理分页功能
//     * @param req
//     * @param resp
//     * @throws ServletException
//     * @throws IOException
//     */
//    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1 获取请求的参数 pageNo 和 pageSize
//        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"), 1);
//        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
//        //2 调用BookService.page(pageNo，pageSize)：Page对象
//        Page<Flowers> page = flowerService.page(pageNo,pageSize);
//
//        page.setUrl("manager/bookServlet?action=page");
//
//        //3 保存Page对象到Request域中
//        req.setAttribute("page",page);
//        //4 请求转发到pages/manager/book_manager.jsp页面
//        req.getRequestDispatcher("/manager/goods_list.jsp").forward(req,resp);
//    }

    protected void addFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        int pageNo = WebUtils.parseInt("pageNo", 0);
//        pageNo+=1;

        //获取请求参数 封装为Flowers对象
        Flowers flowers = WebUtils.copyParamToBean(req.getParameterMap(), new Flowers());
        //调用方法add方法保存商品
        flowerService.addFlower(flowers);

//        resp.sendRedirect(req.getContextPath()+"/manager/flowerServlet?action=page&pageNo="+pageNo);

    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  1、获取请求的参数
        int id = 0;
        String name = null;//商品名称
        int sort_id = 0;//分类sort_id
        String material = null;//材料
        String pack = null;//包装
        String price = null;
        String slogan = null;//花语
        String img_path = null;//图片


        //1.先判断上传的数据是否是多段数据(只有是多段的数据才是文件上传)
        if (ServletFileUpload.isMultipartContent(req)){
            //创建FileItemFactory工厂实现类
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            //创建用于解析的上传数据的工具类ServletFileUpload类
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            //解析上传的数据，得到每一个表单项FileItem
            try {
                List<FileItem> list = servletFileUpload.parseRequest(req);
                //循环判断，每一个表单项，是普通类型，还是上传的文件
                for (FileItem fileItem:list) {
                    if (fileItem.isFormField()){
                        //普通表单项
                        if ("name".equals(fileItem.getFieldName())){
                            System.out.println("商品名称："+fileItem.getString("UTF-8"));
                            name = fileItem.getString("UTF-8");
                        }
                        else if ("id".equals(fileItem.getFieldName())){
                            System.out.println("id："+fileItem.getString("UTF-8"));
                            id = Integer.parseInt(fileItem.getString("UTF-8"));
                        }
                        else if ("material".equals(fileItem.getFieldName())){
                            System.out.println("材料："+fileItem.getString("UTF-8"));
                            material = fileItem.getString("UTF-8");
                        }
                        else if ("pack".equals(fileItem.getFieldName())){
                            System.out.println("包装："+fileItem.getString("UTF-8"));
                            pack = fileItem.getString("UTF-8");
                        }else if ("price".equals(fileItem.getFieldName())){
                            System.out.println("价格："+fileItem.getString("UTF-8"));
                            price = fileItem.getString("UTF-8");
                        }
                        else if ("slogan".equals(fileItem.getFieldName())){
                            System.out.println("花语："+fileItem.getString("UTF-8"));
                            slogan = fileItem.getString("UTF-8");
                        }else if ("sort_id".equals(fileItem.getFieldName())){
                            System.out.println("分类："+fileItem.getString("UTF-8"));
                            sort_id = Integer.parseInt(fileItem.getString("UTF-8"));
                        }
                    }else {
                        //上传文件
                        System.out.println("上传照片："+fileItem.getName());
                        img_path = "img/"+fileItem.getName();
                        fileItem.write(new File("E:\\桌面\\Flower\\out\\artifacts\\Flower_war_exploded\\img\\"+fileItem.getName()));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }



            Flowers flowers = new Flowers(id,name,sort_id,img_path,material,pack,new BigDecimal(price),slogan);
            flowerService.addFlower(flowers);

            req.getSession().setAttribute("flowers",flowers);

            req.getRequestDispatcher("/manager/goods_list.jsp").forward(req, resp);

        }
    }




    protected void deleteFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String pid = req.getParameter("id");
        int id = WebUtils.parseInt(pid, 0);

        flowerService.deleteFlowerById(id);

        req.getRequestDispatcher("/manager/goods_list.jsp").forward(req,resp);

    }


    protected void updateFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Flowers flowers = WebUtils.copyParamToBean(req.getParameterMap(), new Flowers());

        flowerService.updateFlower(flowers);

//        resp.sendRedirect(req.getContextPath()+"/manager/flowerServlet?action=page&pageNo="+req.getParameter("pageNo"));


    }

    protected void getFlower(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = WebUtils.parseInt("id", 0);

        Flowers flower = flowerService.queryFlowerById(id);

        //保存到request域中
        req.setAttribute("flower",flower);


        req.getRequestDispatcher("/manager/goods_list.jsp").forward(req,resp);

    }


    /**
     * 后台 商品列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Flowers> flowers = flowerService.queryFlowers();
        System.out.println(flowers);

        //后台
        Gson gson=new Gson();
        resp.getWriter().write(gson.toJson(flowers));



    }

    /**
     * 前台首页 商品列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pre_list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Flowers> flowers = flowerService.queryFlowers();
        System.out.println(flowers);

        //前台
        req.setAttribute("flowers",flowers);

        req.getRequestDispatcher("pages/Floda/index.jsp").forward(req,resp);


    }

    /**
     * 前台 商品项 中的 商品列表
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void pre_list_shop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Flowers> flowers = flowerService.queryFlowers();
        System.out.println(flowers);

        //前台
        req.setAttribute("flowers",flowers);

        req.getRequestDispatcher("pages/Floda/shop.jsp").forward(req,resp);


    }

    /**
     * 前台 商品详细情况
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void show_details(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String id1 = req.getParameter("id");

        int id = WebUtils.parseInt(id1, 0);

        Flowers flower = flowerService.queryFlowerById(id);

        //保存到request域中
        req.setAttribute("flower_details",flower);

        req.getRequestDispatcher("/pages/Floda/details.jsp").forward(req,resp);


    }






}
